/*
 * Created by the DBAccessor generator.
 * Programmer: Lennart Martens
 * Date: 24/01/2008
 * Time: 16:17:48
 */
package org.hkupp.db.accessors;

import be.proteomics.util.db.interfaces.Deleteable;
import be.proteomics.util.db.interfaces.Persistable;
import be.proteomics.util.db.interfaces.Retrievable;
import be.proteomics.util.db.interfaces.Updateable;

import java.sql.*;
import java.util.HashMap;

/*
 * CVS information:
 *
 * $Revision: 1.4 $
 * $Date: 2007/07/06 09:41:53 $
 */

/**
 * This class is a generated accessor for the Instrument table.
 *
 * @author DBAccessor generator class (Lennart Martens).
 */
public class InstrumentTableAccessor implements Deleteable, Retrievable, Updateable, Persistable {

	/**
	 * This variable tracks changes to the object.
	 */
	protected boolean iUpdated = false;

	/**
	 * This variable can hold generated primary key columns.
	 */
	protected Object[] iKeys = null;

	/**
	 * This variable represents the contents for the 'instrumentid' column.
	 */
	protected long iInstrumentid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'model' column.
	 */
	protected String iModel = null;


	/**
	 * This variable represents the contents for the 'vendor' column.
	 */
	protected String iVendor = null;


	/**
	 * This variable represents the contents for the 'softwarename' column.
	 */
	protected String iSoftwarename = null;


	/**
	 * This variable represents the contents for the 'softwareversion' column.
	 */
	protected String iSoftwareversion = null;


	/**
	 * This variable represents the contents for the 'creationdate' column.
	 */
	protected java.sql.Timestamp iCreationdate = null;


	/**
	 * This variable represents the contents for the 'modificationdate' column.
	 */
	protected java.sql.Timestamp iModificationdate = null;


	/**
	 * This variable represents the contents for the 'username' column.
	 */
	protected String iUsername = null;


	/**
	 * This variable represents the key for the 'instrumentid' column.
	 */
	public static final String INSTRUMENTID = "INSTRUMENTID";

	/**
	 * This variable represents the key for the 'model' column.
	 */
	public static final String MODEL = "MODEL";

	/**
	 * This variable represents the key for the 'vendor' column.
	 */
	public static final String VENDOR = "VENDOR";

	/**
	 * This variable represents the key for the 'softwarename' column.
	 */
	public static final String SOFTWARENAME = "SOFTWARENAME";

	/**
	 * This variable represents the key for the 'softwareversion' column.
	 */
	public static final String SOFTWAREVERSION = "SOFTWAREVERSION";

	/**
	 * This variable represents the key for the 'creationdate' column.
	 */
	public static final String CREATIONDATE = "CREATIONDATE";

	/**
	 * This variable represents the key for the 'modificationdate' column.
	 */
	public static final String MODIFICATIONDATE = "MODIFICATIONDATE";

	/**
	 * This variable represents the key for the 'username' column.
	 */
	public static final String USERNAME = "USERNAME";




	/**
	 * Default constructor.
	 */
	public InstrumentTableAccessor() {
	}

	/**
	 * This constructor allows the creation of the 'InstrumentTableAccessor' object based on a set of values in the HashMap.
	 *
	 * @param	aParams	HashMap with the parameters to initialize this object with.
	 *		<i>Please use only constants defined on this class as keys in the HashMap!</i>
	 */
	public InstrumentTableAccessor(HashMap aParams) {
		if(aParams.containsKey(INSTRUMENTID)) {
			this.iInstrumentid = ((Long)aParams.get(INSTRUMENTID)).longValue();
		}
		if(aParams.containsKey(MODEL)) {
			this.iModel = (String)aParams.get(MODEL);
		}
		if(aParams.containsKey(VENDOR)) {
			this.iVendor = (String)aParams.get(VENDOR);
		}
		if(aParams.containsKey(SOFTWARENAME)) {
			this.iSoftwarename = (String)aParams.get(SOFTWARENAME);
		}
		if(aParams.containsKey(SOFTWAREVERSION)) {
			this.iSoftwareversion = (String)aParams.get(SOFTWAREVERSION);
		}
		if(aParams.containsKey(CREATIONDATE)) {
			this.iCreationdate = (java.sql.Timestamp)aParams.get(CREATIONDATE);
		}
		if(aParams.containsKey(MODIFICATIONDATE)) {
			this.iModificationdate = (java.sql.Timestamp)aParams.get(MODIFICATIONDATE);
		}
		if(aParams.containsKey(USERNAME)) {
			this.iUsername = (String)aParams.get(USERNAME);
		}
		this.iUpdated = true;
	}


	/**
	 * This constructor allows the creation of the 'InstrumentTableAccessor' object based on a resultset
	 * obtained by a 'select * from Instrument' query.
	 *
	 * @param	aResultSet	ResultSet with the required columns to initialize this object with.
	 * @exception	SQLException	when the ResultSet could not be read.
	 */
	public InstrumentTableAccessor(ResultSet aResultSet) throws SQLException {
		this.iInstrumentid = aResultSet.getLong("instrumentid");
		this.iModel = (String)aResultSet.getObject("model");
		this.iVendor = (String)aResultSet.getObject("vendor");
		this.iSoftwarename = (String)aResultSet.getObject("softwarename");
		this.iSoftwareversion = (String)aResultSet.getObject("softwareversion");
		this.iCreationdate = (java.sql.Timestamp)aResultSet.getObject("creationdate");
		this.iModificationdate = (java.sql.Timestamp)aResultSet.getObject("modificationdate");
		this.iUsername = (String)aResultSet.getObject("username");

		this.iUpdated = true;
	}


	/**
	 * This method returns the value for the 'Instrumentid' column
	 * 
	 * @return	long	with the value for the Instrumentid column.
	 */
	public long getInstrumentid() {
		return this.iInstrumentid;
	}

	/**
	 * This method returns the value for the 'Model' column
	 * 
	 * @return	String	with the value for the Model column.
	 */
	public String getModel() {
		return this.iModel;
	}

	/**
	 * This method returns the value for the 'Vendor' column
	 * 
	 * @return	String	with the value for the Vendor column.
	 */
	public String getVendor() {
		return this.iVendor;
	}

	/**
	 * This method returns the value for the 'Softwarename' column
	 * 
	 * @return	String	with the value for the Softwarename column.
	 */
	public String getSoftwarename() {
		return this.iSoftwarename;
	}

	/**
	 * This method returns the value for the 'Softwareversion' column
	 * 
	 * @return	String	with the value for the Softwareversion column.
	 */
	public String getSoftwareversion() {
		return this.iSoftwareversion;
	}

	/**
	 * This method returns the value for the 'Creationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Creationdate column.
	 */
	public java.sql.Timestamp getCreationdate() {
		return this.iCreationdate;
	}

	/**
	 * This method returns the value for the 'Modificationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Modificationdate column.
	 */
	public java.sql.Timestamp getModificationdate() {
		return this.iModificationdate;
	}

	/**
	 * This method returns the value for the 'Username' column
	 * 
	 * @return	String	with the value for the Username column.
	 */
	public String getUsername() {
		return this.iUsername;
	}

	/**
	 * This method sets the value for the 'Instrumentid' column
	 * 
	 * @param	aInstrumentid	long with the value for the Instrumentid column.
	 */
	public void setInstrumentid(long aInstrumentid) {
		this.iInstrumentid = aInstrumentid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Model' column
	 * 
	 * @param	aModel	String with the value for the Model column.
	 */
	public void setModel(String aModel) {
		this.iModel = aModel;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Vendor' column
	 * 
	 * @param	aVendor	String with the value for the Vendor column.
	 */
	public void setVendor(String aVendor) {
		this.iVendor = aVendor;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Softwarename' column
	 * 
	 * @param	aSoftwarename	String with the value for the Softwarename column.
	 */
	public void setSoftwarename(String aSoftwarename) {
		this.iSoftwarename = aSoftwarename;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Softwareversion' column
	 * 
	 * @param	aSoftwareversion	String with the value for the Softwareversion column.
	 */
	public void setSoftwareversion(String aSoftwareversion) {
		this.iSoftwareversion = aSoftwareversion;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Creationdate' column
	 * 
	 * @param	aCreationdate	java.sql.Timestamp with the value for the Creationdate column.
	 */
	public void setCreationdate(java.sql.Timestamp aCreationdate) {
		this.iCreationdate = aCreationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Modificationdate' column
	 * 
	 * @param	aModificationdate	java.sql.Timestamp with the value for the Modificationdate column.
	 */
	public void setModificationdate(java.sql.Timestamp aModificationdate) {
		this.iModificationdate = aModificationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Username' column
	 * 
	 * @param	aUsername	String with the value for the Username column.
	 */
	public void setUsername(String aUsername) {
		this.iUsername = aUsername;
		this.iUpdated = true;
	}



	/**
	 * This method allows the caller to delete the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int delete(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("DELETE FROM instrument WHERE instrumentid = ?");
		lStat.setLong(1, iInstrumentid);
		int result = lStat.executeUpdate();
		lStat.close();
		return result;
	}


	/**
	 * This method allows the caller to read data for this
	 * object from a persistent store based on the specified keys.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public void retrieve(Connection aConn, HashMap aKeys) throws SQLException {
		// First check to see whether all PK fields are present.
		if(!aKeys.containsKey(INSTRUMENTID)) {
			throw new IllegalArgumentException("Primary key field 'INSTRUMENTID' is missing in HashMap!");
		} else {
			iInstrumentid = ((Long)aKeys.get(INSTRUMENTID)).longValue()
;		}
		// In getting here, we probably have all we need to continue. So let's...
		PreparedStatement lStat = aConn.prepareStatement("SELECT * FROM instrument WHERE instrumentid = ?");
		lStat.setLong(1, iInstrumentid);
		ResultSet lRS = lStat.executeQuery();
		int hits = 0;
		while(lRS.next()) {
			hits++;
			iInstrumentid = lRS.getLong("instrumentid");
			iModel = (String)lRS.getObject("model");
			iVendor = (String)lRS.getObject("vendor");
			iSoftwarename = (String)lRS.getObject("softwarename");
			iSoftwareversion = (String)lRS.getObject("softwareversion");
			iCreationdate = (java.sql.Timestamp)lRS.getObject("creationdate");
			iModificationdate = (java.sql.Timestamp)lRS.getObject("modificationdate");
			iUsername = (String)lRS.getObject("username");
		}
		lRS.close();
		lStat.close();
		if(hits>1) {
			throw new SQLException("More than one hit found for the specified primary keys in the 'instrument' table! Object is initialized to last row returned.");
		} else if(hits == 0) {
			throw new SQLException("No hits found for the specified primary keys in the 'instrument' table! Object is not initialized correctly!");
		}
	}


	/**
	 * This method allows the caller to update the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int update(Connection aConn) throws SQLException {
		if(!this.iUpdated) {
			return 0;
		}
		PreparedStatement lStat = aConn.prepareStatement("UPDATE instrument SET instrumentid = ?, model = ?, vendor = ?, softwarename = ?, softwareversion = ?, creationdate = ?, modificationdate = CURRENT_TIMESTAMP, username = ? WHERE instrumentid = ?");
		lStat.setLong(1, iInstrumentid);
		lStat.setObject(2, iModel);
		lStat.setObject(3, iVendor);
		lStat.setObject(4, iSoftwarename);
		lStat.setObject(5, iSoftwareversion);
		lStat.setObject(6, iCreationdate);
		lStat.setObject(7, iUsername);
		lStat.setLong(8, iInstrumentid);
		int result = lStat.executeUpdate();
		lStat.close();
		this.iUpdated = false;
		return result;
	}


	/**
	 * This method allows the caller to insert the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int persist(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("INSERT INTO instrument (instrumentid, model, vendor, softwarename, softwareversion, creationdate, modificationdate, username) values(?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_USER)");
		if(iInstrumentid == Long.MIN_VALUE) {
			lStat.setNull(1, 4);
		} else {
			lStat.setLong(1, iInstrumentid);
		}
		if(iModel == null) {
			lStat.setNull(2, 12);
		} else {
			lStat.setObject(2, iModel);
		}
		if(iVendor == null) {
			lStat.setNull(3, 12);
		} else {
			lStat.setObject(3, iVendor);
		}
		if(iSoftwarename == null) {
			lStat.setNull(4, 12);
		} else {
			lStat.setObject(4, iSoftwarename);
		}
		if(iSoftwareversion == null) {
			lStat.setNull(5, 12);
		} else {
			lStat.setObject(5, iSoftwareversion);
		}
		int result = lStat.executeUpdate();

		// Retrieving the generated keys (if any).
		ResultSet lrsKeys = lStat.getGeneratedKeys();
		ResultSetMetaData lrsmKeys = lrsKeys.getMetaData();
		int colCount = lrsmKeys.getColumnCount();
		iKeys = new Object[colCount];
		while(lrsKeys.next()) {
			for(int i=0;i<iKeys.length;i++) {
				iKeys[i] = lrsKeys.getObject(i+1);
			}
		}
		lrsKeys.close();
		lStat.close();
		this.iUpdated = false;
		return result;
	}

	/**
	 * This method will return the automatically generated key for the insert if 
	 * one was triggered, or 'null' otherwise.
	 *
	 * @return	Object[]	with the generated keys.
	 */
	public Object[] getGeneratedKeys() {
		return this.iKeys;
	}

}